//
//  LeadPageViewController.m
//  SheShiDu
//
//  Created by 单剑秋 on 16/6/21.
//  Copyright © 2016年 单剑秋. All rights reserved.
//

#import "LeadPageViewController.h"
#import "AppDelegate.h"
#import "HomeViewController.h"

@interface LeadPageViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIPageControl *leadPageControl;
@property (nonatomic, strong) NSMutableArray *imageArr;
@property (nonatomic, strong) UIButton *jumpBtn;

@end

@implementation LeadPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addScrolView];
    [self addJumpButton];
}

- (void)addScrolView
{
    
    self.leadScrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _leadScrollView.delegate = self;
    _leadScrollView.bounces = NO;
    _leadScrollView.pagingEnabled = YES;
    _leadScrollView.showsVerticalScrollIndicator = NO;
    _leadScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, SCREEN_HEIGHT);
    [self.view addSubview:_leadScrollView];
    
    // 添加图片到scollView
    for (NSInteger i = 0; i < 3; i++) {
        UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSString *leadStr = [NSString stringWithFormat:@"leadPage%ld.png", i];
        imageV.userInteractionEnabled = YES;
        imageV.image = [UIImage imageNamed:leadStr];
        [self.leadScrollView addSubview:imageV];
    }
    
    self.leadPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 20, SCREEN_HEIGHT * 1214 / 1334, 40, 25)];
    _leadPageControl.backgroundColor = [UIColor clearColor];
    _leadPageControl.numberOfPages = 3;
    [_leadPageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_leadPageControl];
    
    UIButton *joinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    joinBtn.frame = CGRectMake(SCREEN_WIDTH * 2 + SCREEN_WIDTH / 2 - 40, SCREEN_HEIGHT * 1165 / 1334, 80, 30);
    [joinBtn setTitle:@"马上登陆" forState:UIControlStateNormal];
    joinBtn.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:195 / 255.0 blue:183 / 255.0 alpha:1];
    joinBtn.layer.masksToBounds = YES;
    joinBtn.layer.cornerRadius = 5;
    [self.leadScrollView addSubview:joinBtn];
    [joinBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)addJumpButton
{
    self.jumpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _jumpBtn.frame = CGRectMake(SCREEN_WIDTH - 35, 20, 30, 20);
    [_jumpBtn setTitle:@"跳过" forState:UIControlStateNormal];
    _jumpBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    _jumpBtn.backgroundColor = [UIColor lightGrayColor];
    _jumpBtn.layer.masksToBounds = YES;
    _jumpBtn.layer.cornerRadius = 5;
    [_jumpBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_jumpBtn];
}

- (void)buttonAction
{
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    [UIView animateWithDuration:1.0 animations:^{
        _leadScrollView.alpha = 0;
        [self presentViewController:app.tabBarController animated:YES completion:^{
            
        }];
    } completion:^(BOOL finished) {

        
        app.window.rootViewController = app.tabBarController;
        
    }];
}


- (void)pageAction:(UIPageControl *)pageC
{
    [self.leadScrollView setContentOffset:CGPointMake(SCREEN_WIDTH * _leadPageControl.currentPage, 0)];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.leadPageControl.currentPage = self.leadScrollView.contentOffset.x / SCREEN_WIDTH;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
